from PIL import Image


# with Image.open("AnchorageSummer.bmp") as im:
#     im.show()
#
# print(im.width,im.height)

class Imgtool:
    def __init__(self, img_path:None):
        if type(img_path) == str:
            self.image = Image.open(img_path)
        else:
            self.image = Image.new(mode = "RGB",size=(320,240),color=0xFFFFFF)
        self.printInfo()

    def resizeTo_4_3(self):
        scale = self.image.width / self.image.height
        if scale > 1.34:
            print("图片长度更长")
            size_scale = self.image.height / 240
            self.image = self.image.resize((int(self.image.width / size_scale), 240))
            self.image = self.image.crop(((int(self.image.width / 2) - 160), 0,
                                          (int(self.image.width / 2) + 160), 240))

            pass
        elif scale < 1.33:
            print("图片更宽")
            size_scale = self.image.width / 320
            self.image = self.image.resize((320, (int(self.image.height / size_scale))))
            self.image = self.image.crop(
                (
                    0, (int(self.image.width / 2) - 120), 320, (int(self.image.width / 2) + 120)))

        else:
            print("图片不宽不长")
            self.image = self.image.resize((320, 240))

            pass

    def printInfo(self):
        print(f"图片尺寸:{self.image.width}*{self.image.height}")

    def toRGB565_LH_list(self):
        rgb565_lh_list = list()
        for h in range(self.image.height):
            for w in range(self.image.width):
                rgb = self.image.getpixel((w, h))
                r565 = (rgb[0] & 0x00ff) >> 3
                g565 = (rgb[1] & 0x00ff) >> 2
                b565 = (rgb[2] & 0x00ff) >> 3
                rgb565 = (r565 << 11) | (g565 << 5) | b565

                rgb565_lh_list.append((rgb565 & 0xff00) >> 8)
                rgb565_lh_list.append(rgb565 & 0x00ff)

        return rgb565_lh_list

class Album:
    def __init__(self,AlbumName,AlbumTag,picnum):
        self.AlbumName = AlbumName
        self.AlbumTag = AlbumTag
        self.picnum = picnum

class GalleryTool:
    def __init__(self):
        self.AlbumDict = dict()

    def append(self,newAlbumName,newAlbumTag,newAlbum_picnum):
        self.AlbumDict[newAlbumName] = Album(AlbumName=newAlbumName,AlbumTag=newAlbumTag,picnum=newAlbum_picnum)

    def print(self):
        for name in self.AlbumDict.keys():
            print(f"相册名:{self.AlbumDict[name].AlbumName}//相册标识:{self.AlbumDict[name].AlbumTag}//相片数量:{self.AlbumDict[name].picnum}")

    def load(self,addr):
        with open(addr,'r',encoding='utf8') as f:
            albumList = f.readlines()
            for album in albumList:
                album = album[0:-1]
                album_inf = album.split('|')
                if len(album_inf) == 3:
                    self.append(album_inf[0],album_inf[1],int(album_inf[2]))
                    print(album_inf)
    def returnPicList(self,name):
        picList = list()
        if name in self.AlbumDict.keys():
            for i in range(self.AlbumDict[name].picnum):
                if i<=8 :
                    picList.append(f'PIC_0{i+1}')
                else:
                    picList.append(f'PIC_{i+1}')
        return picList




if __name__ == '__main__':
    # pic = Imgtool("AnchorageSummer.bmp")
    # pic.resizeTo_4_3()
    # pic.printInfo()

    g = GalleryTool()
    g.load("相册规则.txt")
    g.print()
    print(g.returnPicList('屏保'))

    pass

